\ Constants used in the cipher
255 Constant byteMax

\ Variables used in the cipher
variable length
variable modLength
variable i
variable offset

\ Set our buffer to 1
: BUFFER_VIGENERE ( -- ) 1 SET_BUFFER ;

\ Setup for Vigenere cipher
: SETUP_VIGENERE ( -- ) BUFFER_VIGENERE ;

\ Encrypt with Vigenere cipher
: ENCRYPT_VIGENERE ( -- ) SETUP_VIGENERE
	\ get string length
	GET_KEY_STRING_LENGTH length !

	READ_LINE
	0 EOF = IF

	ELSE
		\ Initialize looping variable i
		0 i !

		BEGIN
			\ Determine offset from character in key
			i @ length @ MOD modLength !
			GET_KEY_STRING modLength @ /string drop 1 offset !

			\ Fetch the char value, add the offset to it, and mod it with byteMax
			lineBuffer c@ offset + byteMax MOD lineBuffer c!
			WRITE_LINE

			\ Increment looping variable i
			1 i +!

			READ_LINE
			0 EOF =
		UNTIL
	ENDIF ;

\ Decrypt with Vigenere cipher
: DECRYPT_VIGENERE (  -- ) SETUP_VIGENERE
	\ get string length
	GET_KEY_STRING_LENGTH length !

	READ_LINE
	0 EOF = IF

	ELSE
		\ Initialize looping variable i
		0 i !

		BEGIN
			\ Determine offset from character in key
			i @ length @ MOD modLength !
			GET_KEY_STRING modLength @ /string drop 1 offset !

			\ Fetch the char value, subtract the offset from it, and mod it with byteMax
			lineBuffer c@ offset - byteMax MOD lineBuffer c!
			WRITE_LINE

			\ Increment looping variable i
			1 i +!

			READ_LINE
			0 EOF =
		UNTIL
	ENDIF ;
